package com.ds.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author exploredhiraj
 */
public class OutputGenerator {

    public String generateHtmlOutput(String outputHtmlFileName, List<ProfileDetails> listOfProfiles) throws IOException {
        File outputHtmlFile = new File(outputHtmlFileName + ".html");
        FileWriter fw = new FileWriter(outputHtmlFile);
        fw.write("<table><tr><td></td></tr>");

        //First Line
        fw.write("<tr><th><font face='calibri'>Profile Name</font></th>");
        for (ProfileDetails profileDetails : listOfProfiles) {
            fw.write("<th bgcolor='CYAN'><font face='calibri'>" + profileDetails.getProfileName() + "</font></th>");
        }
        fw.write("</tr>");

        //Second Line
        fw.write("<tr bgcolor='#CCFFFF'><td><font face='calibri'> No Of Properties </font></td>");
        for (ProfileDetails profileDetails : listOfProfiles) {
            fw.write("<td><font face='calibri'>" + profileDetails.getNoOfProperties() + "</font></td>");
        }
        fw.write("</tr>");

        //Subsequent Lines
        StringBuilder temp = new StringBuilder();
        int propertyCounter = 0;
        for (String propertyName : getSetOfAllNodeNames(listOfProfiles)) {
            propertyCounter++;
            if (propertyCounter % 2 == 0) {
                fw.write("<tr onmouseover='Row Number" + propertyCounter + "' bgcolor='#CCFFFF'><td><font face='calibri'>" + propertyName + "</font></td>");
            } else {
                fw.write("<tr onmouseover='Row Number" + propertyCounter + "'><td><font face='calibri'>" + propertyName + "</font></td>");
            }
            for (ProfileDetails profileDetails : listOfProfiles) {
                Map<String, String> nodeMap = profileDetails.getNodeMap();
                if (nodeMap.get(propertyName) == null) {
                    fw.write("<td bgcolor='YELLOW'><font face='calibri'>Property Unavailable</font></td>");
                } else if (nodeMap.get(propertyName).equals("")) {
                    fw.write("<td><font face='calibri'><i><b>Value is Blank</b></i></font></td>");
                } else if (propertyName.equals("properties") || propertyName.equals("profile")) {
                } else {
                    if (temp.toString().equals(nodeMap.get(propertyName)) || profileDetails.equals(listOfProfiles.get(0))) {
                        fw.write("<td><font color='GREEN' face='verdana' size='2'>" + nodeMap.get(propertyName) + "</font></td>");
                    } else {
                        fw.write("<td><font color='RED' face='verdana' size='2'>" + nodeMap.get(propertyName) + "</font></td>");
                    }
                }
                temp.delete(0, temp.length());
                temp.append(nodeMap.get(propertyName));
            }
            temp.delete(0, temp.length());
            fw.write("</tr>");
        }
        fw.write("</table>");
        fw.write("<br/><font face='calibri'>Total Number of properties:<b>" + propertyCounter + "</b></font>");
        fw.flush();
        fw.close();
        return outputHtmlFile.getAbsolutePath();
    }

    public static Set<String> getSetOfAllNodeNames(List<ProfileDetails> listOfProfiles) {
        Set<String> setOfProperties = new TreeSet<String>();
        for (ProfileDetails profileDetails : listOfProfiles) {
            setOfProperties.addAll(profileDetails.getNodeNames());
        }
        return setOfProperties;
    }
}
